package com.jz.community.modulemine.myCard.ui;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.bean.BaseResponseInfo;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.modulemine.R;
import com.jz.community.modulemine.R2;
import com.jz.community.modulemine.myCard.adapter.CardInvalidAdapter;
import com.jz.community.modulemine.myCard.info.MyCardInfo;
import com.jz.community.modulemine.myCard.task.CardDeleteTask;
import com.jz.community.modulemine.myCard.task.GetMyCardListTask;

import butterknife.BindView;

/**
 * Created by Tony.Fan on 2018/8/22 17:19
 */
public class CardInvalidActivity extends BaseMvpActivity {

    @BindView(R2.id.title_back_left)
    ImageView titleBackLeft;
    @BindView(R2.id.title_name)
    TextView titleName;
    @BindView(R2.id.title_right)
    TextView titleRight;
    @BindView(R2.id.title_right_iv)
    ImageView titleRightIv;
    @BindView(R2.id.title_toolbar)
    Toolbar titleToolbar;
    @BindView(R2.id.card_invalid_recyclerView)
    RecyclerView cardInvalidRecyclerView;
    @BindView(R2.id.card_invalid_no_empty)
    LinearLayout card_invalid_no_empty;
    @BindView(R2.id.emptyTxt)
    TextView emptyTxt;

    private CardInvalidAdapter cardInvalidAdapter;

    @Override
    protected int getContentView() {
        return R.layout.module_mine_card_invalid_layout;
    }

    @Override
    protected void initView() {
        super.initView();
        setImmersionBar(titleToolbar);
        titleName.setText("失效卡");
    }

    @Override
    protected void addListener() {
        super.addListener();
        titleBackLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initDatas() {
        super.initDatas();
        getMyCardInfo();
    }

    private void getMyCardInfo() {
        new GetMyCardListTask(this, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                MyCardInfo myCardInfo = (MyCardInfo) o;
                if (!Preconditions.isNullOrEmpty(myCardInfo)) {
                    handleCardInvalidAdapter(myCardInfo);
                }
            }
        }).execute("-2");
    }

    private void handleCardInvalidAdapter(MyCardInfo myCardInfo) {
        if (!Preconditions.isNullOrEmpty(myCardInfo.get_embedded().getContent())) {
            LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
            cardInvalidRecyclerView.setLayoutManager(layoutManager);
            cardInvalidAdapter = new CardInvalidAdapter(myCardInfo.get_embedded().getContent());
            cardInvalidRecyclerView.setAdapter(cardInvalidAdapter);
            SHelper.gone(card_invalid_no_empty);
        } else {
            SHelper.vis(card_invalid_no_empty);
            emptyTxt.setText("暂无卡记录！");
        }

        cardInvalidAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                showDeleteDialog(cardInvalidAdapter.getData().get(position).getId() + "");
            }
        });
    }

    private void showDeleteDialog(final String cardId) {
        new MaterialDialog.Builder(mActivity)
                .title("提示")
                .content("确定删除卡片吗？")
                .positiveText("不删了")
                .negativeText("删除")
                .negativeColor(ContextCompat.getColor(this,R.color.negative_color))
                .positiveColor(ContextCompat.getColor(this,R.color.positive_color))
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog dialog, DialogAction which) {
                        //不删了
                        dialog.dismiss();

                    }
                })
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog dialog, DialogAction which) {
                        //删除
                        deleteCard(cardId);
                        dialog.dismiss();
                    }
                })
                .show();
    }

    private void deleteCard(String cardId) {
        new CardDeleteTask(this, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                BaseResponseInfo baseResponseInfo = (BaseResponseInfo) o;
                if (!Preconditions.isNullOrEmpty(baseResponseInfo)) {
                    if (baseResponseInfo.getCode() == 204) {
                        WpToast.l(CardInvalidActivity.this, "删除成功");
                        getMyCardInfo();
                    } else {
                        WpToast.l(CardInvalidActivity.this, "删除失败");
                    }
                }
            }
        }).execute(cardId);
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }
}
